#include "userMain.h"
#include "userBeepDriver.h"

void userBeepGpioInitFunc(void)
{
    GPIO_InitTypeDef        GPIO_InitStructure;

/* GPIOA, GPIOB and GPIOE Clocks enable */
  	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
/* GPIOA Configuration: Channel 1 as alternate function push-pull */
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
  
}
#define     CYCLE_TIME      500
uint16_t BeepChannelPulse = 200,PWM_G_ChannelPulse = 50,PWM_B_ChannelPulse = 300,PWM_W_ChannelPulse = 800;
void userBeepTimerInitFunc(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  	TIM_OCInitTypeDef  TIM_OCInitStructure;
  	uint16_t TimerPeriod = 0;
	uint16_t Channel1Pulse = 700, Channel2Pulse = 50, Channel3Pulse = 300, Channel4Pulse = 800;
 
  /* TIM1 Configuration ---------------------------------------------------
   Generate 7 PWM signals with 4 different duty cycles:
   TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2)    
    => TIM1CLK = PCLK2 = SystemCoreClock
   TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
   SystemCoreClock is set to 48 MHz for STM32F0xx devices
   
   The objective is to generate 7 PWM signal at 17.57 KHz:
     - TIM1_Period = (SystemCoreClock / 17570) - 1
   The channel 1 and channel 1N duty cycle is set to 50%
   The channel 2 and channel 2N duty cycle is set to 37.5%
   The channel 3 and channel 3N duty cycle is set to 25%
   The channel 4 duty cycle is set to 12.5%
   The Timer pulse is calculated as follows:
     - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
   
   Note: 
    SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
    Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
    function to update SystemCoreClock variable value. Otherwise, any configuration
    based on this variable will be incorrect. 
  ----------------------------------------------------------------------- */
  /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
  TimerPeriod = (SystemCoreClock / (6 * CYCLE_TIME) ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
  Channel1Pulse = (uint16_t) (((uint32_t) BeepChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
  Channel2Pulse = (uint16_t) (((uint32_t) PWM_G_ChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
  Channel3Pulse = (uint16_t) (((uint32_t) PWM_B_ChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  Channel4Pulse = (uint16_t) (((uint32_t) PWM_W_ChannelPulse * (TimerPeriod- 1)) / CYCLE_TIME);

  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 1, 2,3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

/***************************************************
  //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;       //compensate with Channel1
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
 // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
  TIM_OC4Init(TIM1, &TIM_OCInitStructure);
****************************************************************/
  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}


void userSetBeepDutyFunc(void)
{
	uint16_t TimerPeriod = 0;
	uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
   /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
  	TimerPeriod = (SystemCoreClock / (6 * CYCLE_TIME) ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
  Channel1Pulse = (uint16_t) (((uint32_t) BeepChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
  Channel2Pulse = (uint16_t) (((uint32_t) PWM_G_ChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
  Channel3Pulse = (uint16_t) (((uint32_t) PWM_B_ChannelPulse * (TimerPeriod - 1)) / CYCLE_TIME);
  /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  Channel4Pulse = (uint16_t) (((uint32_t) PWM_W_ChannelPulse * (TimerPeriod- 1)) / CYCLE_TIME);
  TIM_SetCompare1(TIM1,Channel1Pulse);
  TIM_SetCompare2(TIM1,Channel2Pulse);
  TIM_SetCompare3(TIM1,Channel3Pulse);
  TIM_SetCompare4(TIM1,Channel4Pulse);
}

void userBeepInitialFunc(void)
{
    userBeepGpioInitFunc();
    userBeepTimerInitFunc();
    userBeepOffFunc();
}

void userBeepOnFunc(void)
{
    BeepChannelPulse = 100;  
    //userSetBeepDutyFunc();
    userBeepGpioInitFunc();
    userBeepTimerInitFunc();
}

void userBeepOffFunc(void)
{
    BeepChannelPulse =0;//500;  
    //userSetBeepDutyFunc();
    userBeepGpioInitFunc();
    userBeepTimerInitFunc();
}
